﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using RBS.Common;
using RBS.Core.Model;
namespace RBS.Core.BLL
{
    //换桌，预定，
    public partial class D_Order
    {

        private readonly RBS.Core.DAL.D_Order dal = new RBS.Core.DAL.D_Order();
        public D_Order()
        { }

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RBS.Core.Model.D_Order model)
        {
            return dal.Add(model);

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBS.Core.Model.D_Order model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBS.Core.Model.D_Order GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public RBS.Core.Model.D_Order GetModelByCache(int Id)
        {

            string CacheKey = "D_OrderModel-" + Id;
            object objModel = RBS.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int ModelCache = RBS.Common.ConfigHelper.GetConfigInt("ModelCache");
                        RBS.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (RBS.Core.Model.D_Order)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RBS.Core.Model.D_Order> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RBS.Core.Model.D_Order> DataTableToList(DataTable dt)
        {
            List<RBS.Core.Model.D_Order> modelList = new List<RBS.Core.Model.D_Order>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                RBS.Core.Model.D_Order model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new RBS.Core.Model.D_Order();
                    if (dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    if (dt.Rows[n]["OrderState"].ToString() != "")
                    {
                        model.OrderState = int.Parse(dt.Rows[n]["OrderState"].ToString());
                    }
                    if (dt.Rows[n]["OrderTime"].ToString() != "")
                    {
                        model.OrderTime = DateTime.Parse(dt.Rows[n]["OrderTime"].ToString());
                    }
                    model.OrderChannel = dt.Rows[n]["OrderChannel"].ToString();
                    model.ContactUser = dt.Rows[n]["ContactUser"].ToString();
                    model.ContactMobile = dt.Rows[n]["ContactMobile"].ToString();
                    model.OrderRemark = dt.Rows[n]["OrderRemark"].ToString();
                    if (dt.Rows[n]["OrderAmount"].ToString() != "")
                    {
                        model.OrderAmount = decimal.Parse(dt.Rows[n]["OrderAmount"].ToString());
                    }
                    if (dt.Rows[n]["RecAmount"].ToString() != "")
                    {
                        model.RecAmount = decimal.Parse(dt.Rows[n]["RecAmount"].ToString());
                    }
                    if (dt.Rows[n]["DisAmount"].ToString() != "")
                    {
                        model.DisAmount = decimal.Parse(dt.Rows[n]["DisAmount"].ToString());
                    }
                    if (dt.Rows[n]["AccAmount"].ToString() != "")
                    {
                        model.AccAmount = decimal.Parse(dt.Rows[n]["AccAmount"].ToString());
                    }
                    model.PayType = dt.Rows[n]["PayType"].ToString();
                    if (dt.Rows[n]["PayTime"].ToString() != "")
                    {
                        model.PayTime = DateTime.Parse(dt.Rows[n]["PayTime"].ToString());
                    }
                    model.ReserveDate = dt.Rows[n]["ReserveDate"].ToString();
                    model.ReserveTime = dt.Rows[n]["ReserveTime"].ToString();
                    if (dt.Rows[n]["DineNum"].ToString() != "")
                    {
                        model.DineNum = int.Parse(dt.Rows[n]["DineNum"].ToString());
                    }
                    if (dt.Rows[n]["H_Id"].ToString() != "")
                    {
                        model.H_Id = int.Parse(dt.Rows[n]["H_Id"].ToString());
                    }
                    model.HotelName = dt.Rows[n]["HotelName"].ToString();
                    if (dt.Rows[n]["DineTable"].ToString() != "")
                    {
                        model.DineTable = int.Parse(dt.Rows[n]["DineTable"].ToString());
                    }
                    model.Dinecondition = dt.Rows[n]["Dinecondition"].ToString();
                    if (dt.Rows[n]["U_Id"].ToString() != "")
                    {
                        model.U_Id = int.Parse(dt.Rows[n]["U_Id"].ToString());
                    }
                    if (dt.Rows[n]["CreateTime"].ToString() != "")
                    {
                        model.CreateTime = DateTime.Parse(dt.Rows[n]["CreateTime"].ToString());
                    }
                    model.CreateUser = dt.Rows[n]["CreateUser"].ToString();
                    if (dt.Rows[n]["LastModifyDate"].ToString() != "")
                    {
                        model.LastModifyDate = DateTime.Parse(dt.Rows[n]["LastModifyDate"].ToString());
                    }
                    model.LastModifyUser = dt.Rows[n]["LastModifyUser"].ToString();


                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }
        #endregion


        #region  ExtensionMethod
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return dal.GetList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        #endregion  ExtensionMethod


        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool UpdateOrderStatus(int id, int status)
        {
            return dal.UpdateOrderStatus(id, status);
        }



        /// <summary>
        /// 判断当前座位是否有订单
        /// </summary>
        /// <param name="sid"></param>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public bool Seat(int sid, DateTime time1, DateTime time2)
        {
            return dal.Seat(sid, time1, time2); 
        }

       
    }
}